package leetcode.method.dynamic_programming;

/**
 * 接触题没有思路
 */
public class Program123BuyAndSellTicket {

    public Program123BuyAndSellTicket() {
    }

    public int maxProfit(int[] prices) {
        if (prices.length < 2) {
            return 0;
        }
        int buy1 = -prices[0], sell1 = 0;
        int buy2 = -prices[0], sell2 = 0;

        for (int i = 1; i < prices.length; i++) {
            // 不要关注数组中最大最小值

            // 选择买
            buy1 = Math.max(buy1, -prices[i]);
            // 选择卖
            sell1 = Math.max(sell1, buy1 + prices[i]);

            // 选择买
            buy2 = Math.max(buy2, sell1 - prices[i]);
            //选择，卖
            sell2 = Math.max(sell2, buy2 + prices[i]);
        }
        return sell2;
    }

    public static void main(String[] args) {
        Program123BuyAndSellTicket myStack = new Program123BuyAndSellTicket();
        int[] prices = {3,3,5,0,0,3,1,4};
        System.out.println(myStack.maxProfit(prices));
        int[] prices1 = {1,2,3,4,5};
        System.out.println(myStack.maxProfit(prices1));
        int[] prices2 = {7,6,4,3,1};
        System.out.println(myStack.maxProfit(prices2));
    }
}
